package com.hspedu.homework;

/**
 * hashset 课后练习2
 */
public class Homework07 {

    public static void main(String[] args) {
        java.util.HashSet hashSet = new java.util.HashSet();
        hashSet.add(new Employee("zhangsan", 1000.0, new MyDate(2004, 12, 30)));
        hashSet.add(new Employee("lisi", 1000.0, new MyDate(2003, 2, 15)));
        hashSet.add(new Employee("zhangsan", 3000.0, new MyDate(2004, 12, 30)));

        System.out.println(hashSet);
    }
}

class Employee {
    private String name;
    private double salary;

    private MyDate birthday;

    public Employee(String name, double salary, com.hspedu.homework.MyDate myDate) {
        this.name = name;
        this.salary = salary;
        this.birthday = myDate;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", myDate=" + birthday +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        com.hspedu.homework.Employee employee = (com.hspedu.homework.Employee) o;

        if (!java.util.Objects.equals(name, employee.name)) return false;
        return java.util.Objects.equals(birthday, employee.birthday);
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
        return result;
    }
}

class MyDate {
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }


    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        com.hspedu.homework.MyDate myDate = (com.hspedu.homework.MyDate) o;

        if (year != myDate.year) return false;
        if (month != myDate.month) return false;
        return day == myDate.day;
    }

    @Override
    public int hashCode() {
        int result = year;
        result = 31 * result + month;
        result = 31 * result + day;
        return result;
    }
}